package com.duguyuan.server;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ServerFrame extends JFrame {

    private static final int SERVER_WIDTH = 1200;
    private static final int SERVER_HEIGHT = 800;
    private static final Font TEXT_FONT = new Font("黑体", Font.PLAIN, 16);
    private static final Font LABEL_FONT = new Font("黑体", Font.BOLD, 18);
    private static final Font TEXTFIELD_FONT = new Font("黑体", Font.BOLD, 18);
    private static final Font BUTTON_FONT = new Font("黑体", Font.BOLD, 18);

    private ServerService serverService;

    private ServerView serverView;
    // setPanel 面板组件
    JTextField ipTextField;
    JTextField portTextField;
    JButton startServerButton;
    JButton stopServerButton;
    // userListPanel 面板组件
    JTextField onlineUsersTextField;
    DefaultListModel<String> usersListModel;
    JList<String> usersList;
    // logPanel 面板组件
    JTextArea logTextArea;
    JButton clearButton;

    public ServerFrame() {
        this.serverService = new ServerService(this);
        init();
    }

    public void init() {
        this.setTitle("服务器界面");
        this.setSize(SERVER_WIDTH, SERVER_HEIGHT);
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        serverView = new ServerView();
        this.setContentPane(serverView);
        this.addActionListener(); // 组件添加事件监听

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    /**
     * 为组件添加事件监听
     */
    public void addActionListener() {
        startServerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (serverService.serverIsStarted) {
                    JOptionPane.showMessageDialog(ServerFrame.this, "服务器已启动，无法重新启动", "服务器消息提示", JOptionPane.INFORMATION_MESSAGE);
                }else {
                    serverService.ipAddress = ipTextField.getText();
                    serverService.serverPort = Integer.parseInt(portTextField.getText());
                    new Thread() {
                        @Override
                        public void run() {
                           serverService.openServer();
                        }
                    }.start();
                    serverService.serverIsStarted = true;
                }
            }
        });
        stopServerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = JOptionPane.showConfirmDialog(ServerFrame.this, "是否确认停止服务器并退出?", "服务器消息提示", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                logTextArea.setText("");
            }
        });
    }

    private class ServerView extends JLayeredPane {

        private JPanel setPanel;
        private JPanel userListPanel;
        private JPanel logPanel;

        public ServerView() {
            this.init();
        }

        public void init() {
            this.setBounds(0, 0, SERVER_WIDTH, SERVER_HEIGHT);
            this.addElement();
        }

        public void addElement() {
            // 服务器设置面板
            setPanel = new JPanel(null);
            setPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "服务器设置", TitledBorder.CENTER, TitledBorder.TOP, TEXT_FONT));
            setPanel.setBounds(50, 10, 500, 200);
            this.add(setPanel, 0);
            // IP地址标签
            JLabel ipLabel = new JLabel("IP地址: ");
            ipLabel.setBounds(20, 50, 80, 30);
            ipLabel.setFont(LABEL_FONT);
            setPanel.add(ipLabel);
            // IP地址输入框
            ipTextField = new JTextField(serverService.ipAddress);
            ipTextField.setBounds(100, 50, 160, 30);
            ipTextField.setFont(TEXTFIELD_FONT);
            setPanel.add(ipTextField);
            // 端口号标签
            JLabel portLabel = new JLabel("端口号: ");
            portLabel.setBounds(280, 50, 80, 30);
            portLabel.setFont(LABEL_FONT);
            setPanel.add(portLabel);
            // 端口号输入框
            portTextField = new JTextField(serverService.serverPort + "");
            portTextField.setBounds(360, 50, 80, 30);
            portTextField.setFont(TEXTFIELD_FONT);
            setPanel.add(portTextField);
            // 启动服务器按钮
            startServerButton = new JButton("启动服务器");
            startServerButton.setBounds(80, 120, 120, 30);
            startServerButton.setFont(BUTTON_FONT);
            startServerButton.setBorder(BorderFactory.createRaisedBevelBorder());
            setPanel.add(startServerButton);
            // 停止服务器按钮
            stopServerButton = new JButton("停止服务器");
            stopServerButton.setBounds(300, 120, 120, 30);
            stopServerButton.setFont(BUTTON_FONT);
            stopServerButton.setBorder(BorderFactory.createRaisedBevelBorder());
            setPanel.add(stopServerButton);

            // 用户列表面板
            userListPanel = new JPanel(null);
            userListPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "用户列表", TitledBorder.CENTER, TitledBorder.TOP, TEXT_FONT));
            userListPanel.setBounds(50, 240, 500, 500);
            this.add(userListPanel, 0);
            // 在线用户数量标签
            JLabel onlineUsersLabel = new JLabel("在线用户数量: ");
            onlineUsersLabel.setBounds(120, 30, 150, 30);
            onlineUsersLabel.setFont(LABEL_FONT);
            userListPanel.add(onlineUsersLabel);
            // 在线用户数量显示框
            onlineUsersTextField = new JTextField(ServerFrame.this.serverService.onlineUserCount + "");
            onlineUsersTextField.setBounds(270, 30, 100, 30);
            onlineUsersTextField.setFont(TEXTFIELD_FONT);
            onlineUsersTextField.setEditable(false);
            userListPanel.add(onlineUsersTextField);
            // 在线用户显示列表
            usersListModel = new DefaultListModel<String>();
            usersList = new JList<String>(usersListModel);
            usersList.setFont(new Font("黑体", Font.PLAIN, 16));
            JScrollPane usersListScrollPane = new JScrollPane(usersList);
            usersListScrollPane.setBounds(100, 70, 300, 400);
            userListPanel.add(usersListScrollPane);

            // 服务器日志信息面板
            logPanel = new JPanel(null);
            logPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "服务器日志信息", TitledBorder.CENTER, TitledBorder.TOP, TEXT_FONT));
            logPanel.setBounds(600, 10, 520, 730);
            this.add(logPanel, 0);
            // 日志信息文本域
            logTextArea = new JTextArea();
            logTextArea.setFont(TEXT_FONT);
            logTextArea.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(logTextArea);
            logScrollPane.setBounds(10, 20, 500, 660);
            logPanel.add(logScrollPane);
            // 清屏按钮
            clearButton = new JButton("清屏");
            clearButton.setBounds(430, 690, 80, 30);
            clearButton.setFont(BUTTON_FONT);
            clearButton.setBorder(BorderFactory.createRaisedBevelBorder());
            logPanel.add(clearButton);
        }
    }

}